<?php /* Copyright 2010 Karl R. Wilcox 

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License. */

set_include_path('C:\Users\K Wilcox\Documents\Projects\website\www-new\include\shield');
   
function draw_message($category,$message) { echo $message; }
function get_mod ( $node, $name ) { return null; }
function rgb( $colour ) { return null; }

function get_main_name($patterns) {
  $pattern = $patterns[0];
  $words = explode(' ',$pattern);
  $retval = '';
  foreach ( $words as $word ) {
    if ( $word{0} == '?' ) continue;
    $word = preg_replace('/s\?/','',$word);
    $word = preg_replace('/\?/','',$word);
    $word = preg_replace('/\|.*?\)/',')',$word);
    $word = preg_replace('/\((.*?)\)/','\1',$word);
    $retval .= $word . ' ';
  }
  $retval = trim($retval);
  return $retval;
}

function get_alt_names($patterns, $main = '') {
  $names = array();
  foreach ($patterns as $pattern) {
    $cur_names = array ( '' );
    $words = explode ( ' ', $pattern );
    foreach ( $words as $word ) {
      $word = preg_replace('/s\?/','',$word);
      $word = preg_replace('/\?/','',$word);
      $cur_words = array ( '' );
      $in_brackets = false;
      $temp = '';
      for ( $i = 0; $i < strlen($word); $i++ ) {
        if ( $word{$i} == '(' ) {
          $in_brackets = true;
          $i++;
        }
        elseif ( $word{$i} == ')' ) {
          $alts = explode('|',$temp);
          $orig_num_words = count($cur_words);
          for ( $k = 1; $k < count($alts); $k++ ) {
            for ( $j = 0; $j < $orig_num_words; $j++ )
              $cur_words[] .= $cur_words[0] .  $alts[$k];
          }
          for ( $j = 0; $j < $orig_num_words; $j++ )
            $cur_words[$j] .= $alts[0];
          $temp = '';
          $in_brackets = false;
          $i++;
        }
        if ( $in_brackets )
          $temp .= $word{$i};
        elseif ( $i < strlen($word))
          for ( $j = 0; $j < count($cur_words); $j++ )
            $cur_words[$j] .= $word{$i};
      }
      $orig_num_names = count($cur_names);
      for ( $i = 1; $i < count($cur_words); $i++ )
       for ( $j = 0; $j < $orig_num_names; $j++ )
         $cur_names[] = $cur_names[0] . ' ' . $cur_words[$i];
      for ( $i = 0; $i < $orig_num_names; $i++ )
        $cur_names[$i] .= ' ' . $cur_words[0];
    }
    $names = array_merge($names, $cur_names);
  }
  for ( $i = 0; $i < count($names); $i++ ) {
    $names[$i] = trim($names[$i]);
    if ( $names[$i] == $main ) {
      $names[$i] = '';
      continue;
    }
  }
  return $names;
}

$node = null;
$output = '
The list below shows all the charges that drawshield recognises (but cannot necessarily draw yet).
Items in angle brackets < > are cross references for charges that have alternative names.
Items in braces { } are those parts of the charge that can be given a different colour,
and also shows whether there is a default or proper colour available.
Items in square brackets  [  ] shows additional ways in which the charge can be modified.

WARNING! Do not edit this file! It is automatically generated from program data.

==Charge List==

';

$rows = array();
// Read in the parsing code
$dir = opendir('../charges');
while ( ($subdir = readdir($dir)) != false ) {
  if ( $subdir{0} != '.' and is_dir('../charges/' . $subdir) ) {
    $ddir = opendir('../charges/' . $subdir );
    while ( ($file = readdir($ddir)) != false ) {
      if ( ($file{0} != '_') and substr($file,-4) == '.inc' ) {
        $type = $subdir;
        $subtype = substr($file,0,strlen($file)-4);
        $charge = array();
        include '../charges/' . $subdir . '/' . $file;
        $main_name = get_main_name($charge['patterns']);
        $id = preg_replace ( '/ /', '_', $main_name );
        $features = array();
        $others = array();
        if ( array_key_exists( 'modifiers' , $charge ) ) {
          foreach ( $charge['modifiers'] as $mod ) {
            $mod_pattern =  $mod[1];
            if ( $mod[2] == 'feature' )
              $features[] = $mod_pattern;
            else
              $others[] = $mod_pattern;
          }
        }
        // Column 1 - Charge name
        $this_row = "$main_name ";
        // Column 2 - Colour features and information
        if ( count($features) ) {
          $this_row .= ' {';
          $feat_names = get_alt_names($features);
          foreach ( $feat_names as $feat_name )
            $this_row .= "$feat_name, ";
          $this_row .= '}';
        }
        if ( array_key_exists('default_colour',$charge) ) {
          if ( $charge['default_colour'] == false )
            $this_row .= ' {Charge tincture is fixed}';
          else
          $this_row .= ' {If no tincture given, charge will be ' . $charge['default_colour'] . '}';
        }
        if ( array_key_exists('proper',$charge) )
          $this_row .= ' {Charge tincture may be "proper"}';
        // Column 3 - Other modifiers
        if ( count($others) ) {
          $this_row .= '  [';
          $other_names = get_alt_names($others);
          foreach ( $other_names as $other_name )
            $this_row .= "$other_name, ";
          $this_row .= ']';
        }
        // Column 4 - Notes
        if ( array_key_exists('doc',$charge) and $charge['doc'] != 'Stuff...' ) 
          $this_row .= ' Notes: ' . $charge['doc'];
        // Column 5 - example image
/*        if ( false and array_key_exists('body',$charge) ) {
          $svg = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
          $svg .= '<svg version="1.1" xmlns="http://www.w3.org/2000/svg" baseProfile="full" preserveAspectRatio="xMidYMid meet" height="100px" width="100px" viewBox="0 0 ' . $charge['width'] . ' ' . $charge['height'] . '" >';
          $svg .= '<g fill="#3F3F3F">';
          $svg .= $charge['body'];
          if ( array_key_exists('features', $charge))
            foreach($charge['features'] as $extra)
              $svg .= $extra['body'];
          $svg .= '</g>';
          $svg .= '</svg>' . "\n";
         // file_put_contents('images/charges/' . $id . '.svg',$svg);
         // $status = exec ( '"C:\Program Files (x86)\java\jre6\bin\java.exe" -jar "C:\Users\K Wilcox\bin\batik-1.7\batik-rasterizer.jar" "J:\Karl\'s Folder\www\include\shield\tools\images\charges' . '\\' . $id . '.svg" ' );
          $this_row .= ' || [http://www.karlwilcox.com/images/wiki/charges/' . $id . '.png] ||';
        } else
          $this_row .= " || ||"; */
        // Add current row
        $rows[$main_name] = $this_row;
        // Add all alternate names
        $alts = get_alt_names($charge['patterns'],$main_name);
        foreach ( $alts as $alt )
          if ( $alt != '' ) {
              $initial = strtoupper($main_name{0});
            $rows[$alt] = "$alt <See $main_name>";
          }
      }
    }
  }
}

ksort($rows);
$prev = ' ';
foreach ( $rows as $key => $row ) {
  if ( $key{0} != $prev ) {
    $output .= "\n==" . strtoupper($key{0}) . "==\n\n";
//    $output .= "|| *Name* || *Features* || *Other* || *Notes* || *Example* ||\n";
  }
  $prev = $key{0};
  $output .= $row . "\n";
}

/* $output .= '
=Navigation=

Previous [Charge] Top [SupportedHeraldry] Next [ChargeCategory]
'; */
    
file_put_contents('chg_docs.wiki',$output);

?>
